﻿#include "SceneLevelSelect.h"
#include "GameUtil.h"
#include "BtnLevel.h"
#include "StartGameScene.h"


using namespace cocos2d::extension;


SceneLevelSelect::~SceneLevelSelect(void)
{
}


CCScene* SceneLevelSelect::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        SceneLevelSelect *layer = new SceneLevelSelect();
		if (layer && layer->init(scene)) {
			layer->autorelease();

		} else {
			delete layer;
			layer = NULL;
		}

        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);


    } while (0);

    // return the scene
    return scene;
}

bool SceneLevelSelect::init(CCScene *scene){  
	if (!CCLayer::init()) {  
		return false;  
	}

	// init
	_winSize = CCDirector::sharedDirector()->getWinSize();
	this->setTouchEnabled(true);
	this->setKeypadEnabled(true);

	CCLayerColor *layer = CCLayerColor::create(ccc4(64, 0, 64, 0xFF));
	layer->setContentSize(CCSizeMake(1920,720));

	CCScrollView* scrollView = CCScrollView::create(CCSizeMake(960, 720), layer);
	scrollView->setContainer(layer);
	scrollView->setContentSize(CCSizeMake(1920,720)); 
	scrollView->setDirection( kCCScrollViewDirectionHorizontal );
	//scrollView->setAnchorPoint(ccp(0.5, 0.5));
	scrollView->setPosition(ccp(_winSize.width/2 - 480, 0));
	this->addChild(scrollView, 1);

	for(int i = 0; i < 10; i++) {
		for(int j = 1; j <= 5; j++) {
			BtnLevel* btn = BtnLevel::create(i*5 + j, GameUtil::random(1, 3));
			btn->setPosition(ccp(80 + i*130, 80 + (5-j)*130));
			layer->addChild(btn);
		}
	}

	return true;  
} 

void SceneLevelSelect::keyBackClicked()
{
    CCDirector::sharedDirector()->replaceScene(StartGameScene::scene());
}
